<?php
class PM_f
{
    public static function __index( $room_id , $db_connect ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //取出该房间的所有结算信息
        $scoring_info  = $db_connect->select('*')->from('tt_room_scoring')->where("room_id= '". $room_id ."' ")->query();
        //遍历房间内的所有用户;获取他们的结算数据
        $data = array();
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $uid = $value;
                //获取6份数据
                $data[$uid] = array();
                $data[$uid]['hupai_count']   = self::__get_UserOther_Count( $scoring_info , $uid , "win_uid" );
                $data[$uid]['pao_count']     = self::__get_UserOther_Count( $scoring_info , $uid , "pao_uid" );
                $data[$uid]['jiepao_count']  = self::__get_User_JiePao_count( $scoring_info , $uid );
                $data[$uid]['zimo_count']    = self::__get_User_ZIMO_count( $scoring_info , $uid );
                $data[$uid]['zuida_huxi']    = self::__get_Max_Huxi( $scoring_info , $uid );
                $data[$uid]['zuida_fanshu']  = self::__get_Max_Fanshu( $scoring_info , $uid );
                $index                       = PHZ_User::__get_user_Room_Index( $room_info , $value );
                $data[$uid]['zongfen']       = $room_info['player_'. $index .'_score'];
            }
        }
        //获取谁是炮手
        $data['zjps_uid']  = self::__get_ZuiJiaPS( $data );
        //获取谁是大赢家
        $data['dyj_uid']   = self::__get_DaYingJIa( $room_info );
        //设置一些基础信息
        $room_cname        = PHZ_Member::__getFieldBy_ID( $room_info['room_cid'] , "name" , $db_connect );
        $data['room_info'] = array( 'room_id' => $room_id , 'room_cid' => $room_info['room_cid'], 'room_zjs' => $room_info['room_zjs'] , 'room_mtype' => $room_info['room_mtype'] , 'room_cname' => $room_cname );
        $data['end_time']  = date('Y-m-d H:i:s');
        //进行微信机器人的数据推送
        PHZ_VbotShare::__setData( $room_info , $db_connect );
        //返回数据
        return $data;
    }

    public static function __get_DaYingJIa( $room_info ){
        $first_score = $room_info['player_1_score'];
        if( $room_info['player_1_score'] == $first_score && $room_info['player_2_score'] == $first_score && $room_info['player_3_score'] ==  $first_score ){
            return 0;
        }
        $temp_data = array(
            $room_info['room_player_1'] => $room_info['player_1_score'],
            $room_info['room_player_2'] => $room_info['player_2_score'],
            $room_info['room_player_3'] => $room_info['player_3_score']
        );
        arsort( $temp_data );
        foreach ( $temp_data as $key => $value ){return $key;break;}
    }

    public static function __get_ZuiJiaPS( $data ){
        $dyj_data = array();
        foreach ( $data as $key => $value ){
            $dyj_data[$key] = $value['pao_count'];
        }
        $temp_data = array_values($dyj_data);
        if( count( $temp_data ) == 3 ){
            if( $temp_data[0] == $temp_data[1] && $temp_data[1] == $temp_data[2] ){
                return 0;
            }
        }else if( count( $temp_data ) == 4 ){
            if( $temp_data[0] == $temp_data[1] && $temp_data[1] == $temp_data[2] ){
                return 0;
            }
        }
        arsort( $dyj_data );
        foreach ( $dyj_data as $key => $value ){return $key;break;}
    }

    public static function __get_Max_Fanshu( $scoring_info , $user_id ){
        $fanshu_array = array();
        foreach ( $scoring_info as $key => $value ){
            if( $value['win_uid'] == $user_id ){
                array_push( $fanshu_array , $value['plus']  );
            }
        }
        if( count($fanshu_array) <= 0 ){return 0;}
        rsort( $fanshu_array );return $fanshu_array[0];
    }

    public static function __get_Max_Huxi( $scoring_info , $user_id ){
        $huxi_array = array();
        foreach ( $scoring_info as $key => $value ){
            if( $value['win_uid'] == $user_id ){
                if( $value['plus'] <= 0 ){
                    array_push( $huxi_array , $value['hu_xi']  );
                }else{
                    array_push( $huxi_array , $value['hu_xi'] * $value['plus'] );
                }
            }
        }
        if( count($huxi_array) <= 0 ){return 0;}
        rsort( $huxi_array );return $huxi_array[0];
    }

    public static function __get_User_JiePao_count( $scoring_info , $user_id ){
        $count = 0;
        foreach ( $scoring_info as $key => $value ){
            if( $value['win_uid'] == $user_id && $value['mode'] == 0 ){$count++;}
        }
        return $count;
    }

    public static function __get_User_ZIMO_count( $scoring_info , $user_id ){
        $count = 0;
        foreach ( $scoring_info as $key => $value ){
            if( $value['win_uid'] == $user_id && $value['mode'] == 2 ){$count++;}
        }
        return $count;
    }

    public static function __get_UserOther_Count( $scoring_info , $user_id , $type ){
        $count = 0;
        foreach ( $scoring_info as $key => $value ){
            if( $value[ $type ] == $user_id ){$count++;}
        }
        return $count;
    }

}